-- UIFeatureDesc
-- Create by zouyb
-- 引导道具弹窗界面

--UIFeatureDesc继承自Layer
UIFeatureDesc = class("UIFeatureDesc", function()
    return cc.Layer:create();
end);

function UIFeatureDesc.create(classId, pos, callback, extraData)
    return UIFeatureDesc.new(classId, pos, callback, extraData);
end

--构造函数
function UIFeatureDesc:ctor(classId, pos, callback, extraData)
    self:setName("UIFeatureDesc");
    self.classId = classId;
    self.pos = pos;
    self.callback = callback;
    self.extraData = extraData or {};

    --初始化
    self:setLocalZOrder(UIMgr.BONUS_NOTIFY_ZORDER + 1);
    local node = cc.CSLoader:createNode("layout/guide/FeatureDesc.csb");
    self:addChild(node);
    self.node = node;

    -- 注册点击事件
    self:registerTouchEvent();

    -- 初始化窗口
    self:initForm();

    -- 适配
    self:resize();
end

-- 注册点击事件
function UIFeatureDesc:registerTouchEvent()
   -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            if self.extraData.cancel_callback then
                self.extraData.cancel_callback();
            end

            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UIFeatureDesc");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 注册确认按钮点击事件
    local btnOK = findChildByName(self.node, "CT/btn_ok");
    local function onOKClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            if self.callback then
                self.callback();
            end

            local popHint = self.extraData.pop_hint or FeatureItemM.query(self.classId, "pop_hint");
            alert(popHint);

            UIMgr.getCurrentScene():removeFormByName("UIFeatureDesc");
        end
    end
    btnOK:addTouchEventListener(onOKClick);
    TextStyleM.setTextStyle(btnOK, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnOK:setTitleText(getLocStr("btn_text_ok"));
end

-- 初始化窗口
function UIFeatureDesc:initForm()
    -- 标题
    local titleLabel = findChildByName(self.node, "CT/bg1/title");
    TextStyleM.setTitleStyle(titleLabel);
    titleLabel:setString(self.extraData.item_name or FeatureItemM.query(self.classId, "item_name"));

    local subTitleLabel = findChildByName(self.node, "CT/bg1/subhead");
    TextStyleM.setSubheadStyle(subTitleLabel);
    subTitleLabel:setString(self.extraData.sub_name or FeatureItemM.query(self.classId, "sub_name"));

    -- 道具描述
    local descLabel = findChildByName(self.node, "CT/desc");
    TextStyleM.setTextStyle(descLabel,TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    descLabel:getVirtualRenderer():setLineHeight(TextStyleM.TEXT_SIZE_SMALL + 5);
    applyString(descLabel, self.extraData.pop_desc or FeatureItemM.query(self.classId, "pop_desc"));

    -- 图标
    self.icons = {}
    for i = 1, 6 do
        local iconNode = findChildByName(self.node, "CT/icon"..i);
        iconNode:setVisible(false);
        table.insert(self.icons, iconNode);
    end

    local frameBgPath = self.extraData.frame_bg or FeatureItemM.query(self.classId, "frame_bg");
    local frameIconPath = self.extraData.frame_icon or FeatureItemM.query(self.classId, "frame_icon");

    local iconType = self.extraData.icon_type or FeatureItemM.query(self.classId, "icon_type");

    self.icons[iconType]:setVisible(true);

    -- 框的底图
    local frameBg = findChildByName(self.icons[iconType], "frame");
    frameBg:loadTexture(frameBgPath);

    -- 框图标
    local iconBg = findChildByName(self.icons[iconType], "icon");
    iconBg:loadTexture(frameIconPath);

    -- 初始化泡泡中的icon
    local markImg = findChildByName(self.icons[iconType], "mark");
    local bubbleIconType = self.extraData.bubble_icon_type or FeatureItemM.query(self.classId, "bubble_icon_type");
    local _, _, type, para = string.find(bubbleIconType, "(%a+)%((%d+)%)");

    if type == "food" then
        -- 如果是喂食，泡泡中显示地精巧克力的道具图标
        local iconPath = getItemIconPath(ItemM.query(tonumber(para), "icon"));
        markImg:loadTexture(iconPath);
    elseif type == "star" then
        -- 如果是星星，显示para个星星堆叠
        markImg:loadTexture("images/ui/symbol/star_huge.png");
        markImg:setScale(0.7);

        local markClone = markImg:clone();
        local size = markImg:getContentSize();
        for i = 1, tonumber(para) - 1 do
            local starImg = markClone:clone();
            starImg:setPosition(cc.p(size.width / 2 + i * 10, size.height / 2));
            starImg:setScale(1);
            markImg:addChild(starImg);
        end

        markImg:setPositionX(markImg:getPositionX() -(para - 1) * 10 / 2 * markImg:getScaleX() + 1);
    end

    -- 世界树相关的，显示当前冈布奥头像
    if iconType == 3 then
        local heroImg = findChildByName(self.icons[iconType], "slime");

        self.pet = ME.user:getActivePet();
        self:createHeroModel(self.icons[iconType]);
    end

    -- 界面音效
    AudioM.playFx("ui_open1");
end

-- 创建英雄模型
function UIFeatureDesc:createHeroModel(node)
    local headImg   = findChildByName(node, "head_icon");
    local modelNode = findChildByName(node, "model_node");

    -- 获取当前出战英雄
    local iconPath = getHeroLargeIconPath(self.pet:getIcon());
    modelNode:removeAllChildren();
    createHeroModel(self.pet:getModelId(), headImg, modelNode, 0.4, iconPath);
end

function UIFeatureDesc:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end


